Skip to content

[13.x] Check traits in resolveClassAttribute - #60566

Merged
taylorotwell merged 4 commits into
laravel:13.xfrom
jackbayliss:13.x-trait-model-attributes
Jul 22, 2026
Merged

[13.x] Check traits in resolveClassAttribute #60566
taylorotwell merged 4 commits into
laravel:13.xfrom
jackbayliss:13.x-trait-model-attributes

Conversation

@jackbayliss

@jackbayliss jackbayliss commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

This is the exact same as I did here #60519

This means queue attributes 🤝 model attributes (We are same same but different)

This essentially means, traits can carry model attributes which means we can put attributes on a trait, and reuse the trait.

This only checks traits on the current class in the hierarchy same as the other PR

Nice for package maintainers, or teams using traits.

#[Connection('warehouse')]
trait UsesWarehouse
{
  // methods etc... that are shared
}

class Product extends Model
{
    use UsesWarehouse;
}

class StockTransfer extends Model
{
    use UsesWarehouse;
}

I'm completely fine if you close this, just thought I'd try bring the same behaviour as queues to models for the attributes 🫡

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss
jackbayliss marked this pull request as ready for review July 21, 2026 20:29
@taylorotwell
taylorotwell merged commit 23e9e71 into laravel:13.x Jul 22, 2026
53 checks passed
@jackbayliss
jackbayliss deleted the 13.x-trait-model-attributes branch July 22, 2026 16:51
irabbi360 added a commit to irabbi360/framework that referenced this pull request Jul 31, 2026
Fixes laravel#60953

After laravel#60566 / laravel#60519, class attributes like `#[Table]` and `#[Connection]` can live on traits. That works fine, but if two traits on the same class declare the same attribute with different values, we just picked the first trait in the `use` list and moved on.

That means something as harmless as reordering imports / traits can change runtime behaviour, which feels a bit nasty.

This PR makes that conflict explicit instead:

- if multiple traits declare the same attribute with **different** values → throw a `LogicException`
- if they declare the **same** value → still fine
- class-level attributes still win over traits (unchanged)

Shared the trait lookup in `Reflector::getClassAttributeFromTraits()` and wired it into both `Model::resolveClassAttribute()` and `ReadsClassAttributes` so Eloquent and queue attributes stay consistent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants